

package com.soso.base.google.mq;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.soso.base.exception.unchecked.GeneralExceptionHandler;
import com.soso.base.google.mq.Message;
import com.soso.base.google.mq.MessageQueue;
import com.soso.base.util.log.Logger;
import com.soso.base.util.other.SerialUtil;

/**
 * 
 * @author speed
 *
 */
@SuppressWarnings("serial")
public abstract class MessageQueueServlet extends HttpServlet  {

	public static final String MQ_URL = "/_ah/queue/mq";
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		execute(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		execute(request, response);
	}

	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Logger.info("Ping success.");
		try {
			int contentLength=request.getContentLength();
			byte[] bytesIn = new byte[ contentLength ];
			request.getInputStream().readLine( bytesIn, 0, contentLength );
			Message message = (Message)SerialUtil.deserialize(bytesIn);
			getMessageQueue().execute(message);
		}catch(Exception e) {
			GeneralExceptionHandler.log(e);
		}
	}
	public abstract MessageQueue getMessageQueue();
}
